package com.briup.store.service.impl;

import com.briup.common.exception.SystemException;
import com.briup.common.response.CodeAndMessageEnum;
import com.briup.common.utils.BeanCopyUtils;
import com.briup.common.utils.JsonWebTokenUtils;
import com.briup.common.utils.UserContext;
import com.briup.store.domain.dto.UserLoginDTO;
import com.briup.store.domain.dto.UserRegisterDTO;
import com.briup.store.domain.dto.UserUpdateDTO;
import com.briup.store.domain.dto.UserUpdatePasswordDTO;
import com.briup.store.mapper.UserMapper;
import com.briup.store.domain.pojo.User;
import com.briup.store.service.UserService;
import com.briup.store.domain.vo.UserGetInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
* @author qinyc
* @description 针对表【es_user(用户表)】的数据库操作Service实现
* @createDate 2024-12-24 09:16:52
*/
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;


    @Override
    public void register(UserRegisterDTO userRegister) {
        /**
         * 参数校验
         */
        if (!StringUtils.hasText(userRegister.getUsername())){
            //用户名称不能为空
            throw new SystemException(CodeAndMessageEnum.USERNAME_IS_NOT_NULL);
        }
        if (!StringUtils.hasText(userRegister.getPassword())){
            //密码不能为空
            throw new SystemException(CodeAndMessageEnum.PASSWORD_IS_NOT_NULL);
        }

        Integer count = userMapper.selectCountByUsername(userRegister.getUsername());

        if (count >= 1){
            throw new SystemException(CodeAndMessageEnum.DUPLICATE_USERNAMES);
        }

        /**
         * 创建User对象，完成插入数据库
         */
        //Bean拷贝
        User user = BeanCopyUtils.copyBean(userRegister, User.class);

        //设置注册时间
        user.setRegisterTime(LocalDateTime.now());
        //设置用户角色
        user.setRoleId(2);
        //设置VIP状态
        user.setIsVip(false);
        //设置用户账号状态
        user.setStatus(0);

        //新增用户
        userMapper.addUser(user);
    }

    @Override
    public String login(UserLoginDTO userLogin) {
        if (!StringUtils.hasText(userLogin.getUsername())){
            //用户名称不能为空
            throw new SystemException(CodeAndMessageEnum.USERNAME_IS_NOT_NULL);
        }
        if (!StringUtils.hasText(userLogin.getPassword())){
            //密码不能为空
            throw new SystemException(CodeAndMessageEnum.PASSWORD_IS_NOT_NULL);
        }

        //根据用户名查询用户信息
        User user = userMapper.selectUserByUsername(userLogin.getUsername());

        if (Objects.isNull(user)){
            throw new SystemException(CodeAndMessageEnum.USERNAME_INFORMATION_IS_ERROR);
        }

        if (!user.getPassword().equals(userLogin.getPassword())){
            throw new SystemException(CodeAndMessageEnum.PASSWORD_INFORMATION_IS_ERROR);
        }

        //颁发令牌
        String userId = user.getId().toString();
        //System.out.println("in userSerivce,login success,id: " + userId);
        return JsonWebTokenUtils.createJWT(userId);
    }

    @Override
    public UserGetInfoVO getUserInfo(Integer userId) {
        User user = userMapper.getUserById(userId);
        //Bean拷贝并返回
        return BeanCopyUtils.copyBean(user, UserGetInfoVO.class);
    }

    @Override
    public void updateUser(UserUpdateDTO userUpdate) {
        //获取用户ID
        Integer userId = UserContext.getUser();

        /**
         * 参数校验
         */



        //Bean拷贝
        User user = BeanCopyUtils.copyBean(userUpdate, User.class);
        //设置用户ID
        user.setId(userId);
        /**
         * 更新数据
         */
        userMapper.update(user);
    }

    @Override
    public void updatePassword(UserUpdatePasswordDTO userUpdatePassword) {
        /**
         * 参数校验
         */
        //新密码与确认密码保持一致
        if (!userUpdatePassword.getNewPassword().equals(userUpdatePassword.getConfirmPassword())){
            throw new SystemException(CodeAndMessageEnum.NWE_PASSWORD_CONFIRM_PASSWORD_INCONSISTENT);
        }

        //判断旧密码与数据库中保存的密码是否一致
        User user = userMapper.getUserById(UserContext.getUser());
        if (!user.getPassword().equals(userUpdatePassword.getOldPassword())){
            throw new SystemException(CodeAndMessageEnum.OLD_PASSWORD_DB_PASSWORD_INCONSISTENT);
        }

        //修改密码
        userMapper.updatePassword(userUpdatePassword.getNewPassword(),UserContext.getUser());
    }

    @Override
    public BigDecimal getBalance(Integer currentUserId) {
        BigDecimal balance = userMapper.getBalance(currentUserId);
        return balance;
    }

    @Override
    public void updateBalance(BigDecimal newBalance) {
        userMapper.updateBalance(newBalance,UserContext.getUser());
    }
}




